#ifndef __NATSUME_Singleton_hpp_
#define __NATSUME_Singleton_hpp_

#include <cstdlib>
#include <pthread.h>

namespace NATSUME {

    // 单例模式模板类
    template <typename Object>
    class Singleton {
        public:
            template <typename... Args>
            static Object   *getInstance(Args... args) {
                if (m_pInstance == nullptr) {
                    m_pInstance = new Object(args ...);
                    std::atexit(destroy);
                }

                return m_pInstance;
            }

            static void     destroy() {
                if (m_pInstance) {
                    delete m_pInstance;
                    m_pInstance = nullptr;
                }
            }

        private:
            Singleton() = default;
            ~Singleton() = default;
            
        private:
            // 静态指针
            static Object           *m_pInstance;
            static pthread_once_t   m_once;
    };

    template <typename Object>
    Object  *Singleton<Object>::m_pInstance     = nullptr;

    template <typename Object>
    pthread_once_t  Singleton<Object>::m_once   = PTHREAD_ONCE_INIT;
    
}

#endif // __NATSUME_Singleton_hpp_